package com.example.gyt.controller;

import com.example.gyt.pojo.*;
import com.example.gyt.service.MessagesService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class MessagesJsonController {

    @Autowired
    private MessagesService messagesService;

    //添加消息方法
    @ResponseBody
    @RequestMapping(value = "/member/addMessage",method = RequestMethod.POST)
    public ResultUtil addMessage(@Validated @RequestBody Messages messages, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(messages.getSenderId())){
                return ResultUtil.failure("发送人id不能为空");
            }
            if(StringUtils.isEmpty(messages.getSenderContent())){
                return ResultUtil.failure("发送内容不能为空");
            }
            if(StringUtils.isEmpty(messages.getReceivedId())){
                return ResultUtil.failure("接收人id不能为空");
            }
            if(StringUtils.isEmpty(messages.getReceivedContent())){
                return ResultUtil.failure("接收内容不能为空");
            }

            messages.setId(UuidUtil.getUUID());

            //获取到当前时间
            messages.setCreateTime(DateUtil.getCurrentTime());

            int count = messagesService.insertSelective(messages);
            if (count == 0){
                return ResultUtil.success("添加消息失败");
            }

            return ResultUtil.success("添加消息信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一条消息
    @ResponseBody
    @RequestMapping(value = "/member/deleteMessages",method = RequestMethod.POST)
    public ResultUtil deleteMessages(@Validated @RequestBody Messages messages, BindingResult result){
        try {

            if(StringUtils.isEmpty(messages.getId())){
                return ResultUtil.failure("消息ID不能为空");
            }

            int count = messagesService.deleteByPrimaryKey(messages.getId());
            if (count == 0){
                return ResultUtil.failure("删除消息失败");
            }

            return ResultUtil.success("删除消息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //消息ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryMessages",method = RequestMethod.POST)
    public ResultUtil queryMessages(@Validated @RequestBody Messages messages, BindingResult result){
        try {

            if(StringUtils.isEmpty(messages.getId())){
                return ResultUtil.failure("消息ID不能为空");
            }

            Messages messages1 = messagesService.selectByPrimaryKey(messages.getId());
            if (messages1 == null){
                return ResultUtil.failure("获取消息信息失败");
            }

            return ResultUtil.success(messages1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改消息信息
    @ResponseBody
    @RequestMapping(value = "/member/updateMessages",method = RequestMethod.POST)
    public ResultUtil updateMessages(@Validated @RequestBody Messages messages, BindingResult result){
        try {
            if(StringUtils.isEmpty(messages.getId())){
                return ResultUtil.failure("消息ID不能为空");
            }

            /*if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }*/

            //更新修改时间
            messages.setUpdateTime(DateUtil.getCurrentTime());

            int count = messagesService.updateByPrimaryKeySelective(messages);
            if (count == 0){
                return ResultUtil.failure("修改消息信息失败");
            }

            return ResultUtil.success("修改消息信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }


    //集合于消息分页查询，所有消息查询，消息类型查询，消息名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allMessagesList",method = RequestMethod.POST)
    public ResultUtil allMessagesList(@Validated @RequestBody Messages messages, BindingResult result){
        try {

            List<Messages> messages1 = messagesService.getAllMessagesList(messages);
            if (messages1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = messagesService.queryCountLike(messages);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (messages.getPageResult() != null){
                pageResult.setCurrentPage(messages.getPageResult().getCurrentPage());
                pageResult.setPageSize(messages.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(messages1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
